<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ranking extends CI_Controller {

		function __construct()
	{
		parent::__construct();
		$this->load->library('ion_auth');
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->database();
		$this->load->helper(array('url','language'));
    $this->lang->load('general');
    $this->load->model(array('points_model', 'stages_model', 'riders_model'));
    $this->data['user'] = $this->ion_auth->user()->row();
	}

  function overall()
  {

    $this->data['overall_ranking'] = $this->points_model->get_overall_ranking();
    if($this->data['overall_ranking'])
    {
      // Make data for graph
      $i = 0;
      for ($i=0; $i < 9; $i++) { 
        if (isset($this->data['overall_ranking'][$i]))
        {
          if (is_object($this->data['overall_ranking'][$i]))
          {
            $user[$i] = $this->data['overall_ranking'][$i];
            $user[$i]->points = $this->points_model->get_by_user_id($user[$i]->user_id);
          }
        }
      }

      $series_string = "";
      $x_categories = "";
      foreach ($user as $u)
      {
        $series_string .= "{ name: '".$u->username."', data: [";
        $x_categories = "['";
        $aggregate = 0;
        foreach($u->points as $points)
        {
          $aggregate = $aggregate + $points->points_total;
          $series_string .= $aggregate.", ";
          $x_categories .= $points->points_stage_id."', '";
        }
        $series_string = trim($series_string, ", ");
        $series_string .= "] }, ";
      }
      $this->data['x_categories'] = trim($x_categories,", '")."']";
      $this->data['series_string'] = trim($series_string, ", ");
    }
    else
    {
      $this->data['no_results_yet'] = 1;
    }
    $this->data['title'] = 'Overall Ranking';
    $this->data['view'] = 'ranking/overall';
    $this->load->view('template', $this->data);

  }

  function stage($stage_id)
  {
    $this->data['stage_ranking'] = $this->points_model->get_by_stage_id($stage_id);

    $this->data['stage'] = $this->stages_model->get_by_id($stage_id);

    $stage_result = explode(',', $this->data['stage']->stage_result);

    foreach($stage_result as $pos)
    {
      $this->data['stage_result'][] = $this->riders_model->get_by_id($pos);
    }

    $stage_yellow = explode(',', $this->data['stage']->stage_yellow);

    foreach($stage_yellow as $pos)
    {
      $this->data['stage_yellow'][] = $this->riders_model->get_by_id($pos);
    }    

    $stage_green = explode(',', $this->data['stage']->stage_green);

    foreach($stage_green as $pos)
    {
      $this->data['stage_green'][] = $this->riders_model->get_by_id($pos);
    }  

    $stage_mountain = explode(',', $this->data['stage']->stage_mountain);

    foreach($stage_mountain as $pos)
    {
      $this->data['stage_mountain'][] = $this->riders_model->get_by_id($pos);
    }      
    $this->data['title'] = 'Stage Ranking';
    $this->data['view'] = 'ranking/stage';
    $this->load->view('template', $this->data);

  }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */